package com.zqxq.lottery.api.controller;


import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import com.zqxq.lottery.api.utils.LotteryValidateUtils;
import com.zqxq.lottery.entity.bo.order.UserOrderBO;
import com.zqxq.lottery.entity.o2o.order.UserOrderO2O;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.order.UserOrderAPIVO;
import com.zqxq.lottery.entity.vo.order.UserOrderVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 说    明：   竞猜 接口
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/3/22 17:56
 * 修 改 人：
 * 修改日期：
 **/
@Api(value = "订单接口",tags = "订单接口")
@Slf4j
@RestController
@RequestMapping(value = "/order", produces = "application/json")
public class OrderController {

    // 排列组合
    private long combine(int m, int n) {
        if (m < n || n < 0) {
            return 0L;
        }
        return factorial(m, m - n + 1) / factorial(n, 1);
    }

    // 阶乘
    private long factorial(long max, long min) {
        if (max >= min && max > 1) {
            return max * factorial(max - 1, min);
        } else {
            return 1L;
        }
    }

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @PostMapping("/page")
    @ApiOperation(value = "订单列表查询", notes = "订单列表查询")
    public Result<Page<List<UserOrderVO>>> queryUserOrderPage(UserOrderBO bo) {
        if (null == bo.getUserId())
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        //全部订单【空】 中奖订单【4,5,6】 待开奖订单【2】
        if(null == bo.getStatus()){
            bo.setStatusList(Arrays.asList(0,1,2,3,4,5,6,7));
        }
        if(null != bo.getStatus() && 4==bo.getStatus()){
            bo.setStatus(null);
            bo.setStatusList(Arrays.asList(4,5,6));
        }
        if(null != bo.getStatus() &&2==bo.getStatus()){
            bo.setStatus(null);
            bo.setStatusList(Arrays.asList(2));
        }
        long beginTimes = System.currentTimeMillis();
        Result<Page<List<UserOrderVO>>> result = lotteryServerFeignClient.queryUserOrderPage(bo);
        long endTimes = System.currentTimeMillis();
        log.info("-------------------getOrderListTiems----------------------->"+(endTimes - beginTimes));
        return result;
    }


    @PostMapping("/add")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    public Result<UserOrderVO> addUserOrder(UserOrderAPIVO api) {
        UserOrderVO vo = UserOrderO2O.apiToVO(api);
        //查询下单开关
        SysConfigPO configPO = lotteryServerFeignClient.queryConfigValue("","ODER_CREATE_SWITCH").getData();
        if (configPO != null && "0".equals(configPO.getCval())){
            return new Result(CodeEnum.FAIL.getCode(),"暂停售票");
        }
        LotteryValidateUtils lotteryUtils = new LotteryValidateUtils(lotteryServerFeignClient);
        Result rs = lotteryUtils.validateOrder(vo);
        if (!rs.success())
            return rs;
        return lotteryServerFeignClient.addUserOrder(vo);
    }

    @PostMapping("/pay")
    @ApiOperation(value = "支付订单", notes = "支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "couponId", value = "优惠券Id", required = false, paramType = "query", dataType = "Long")
    })
    public Result<UserOrderVO> payUserOrder(Long userId,Long orderId,Long couponId) {
        return lotteryServerFeignClient.payUserOrder(userId,orderId,couponId);
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改订单状态", notes = "修改订单状态")
    public Result updateStatus(String id, String status) {
        if(StrUtils.isEmpty(status))
            return new Result(CodeEnum.FAIL_100002.getCode(), "订单修改失败！");
        if ("2".equals(status.trim())) {
            return new Result(CodeEnum.FAIL_100002.getCode(), "订单修改失败！");
        }
        return lotteryServerFeignClient.updateUserOrderStatus(id,status);
    }

    @PostMapping("/get")
    @ApiOperation(value = "订单查询详情", notes = "订单查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", paramType = "query", dataType = "Long", required = true)
    })
    public Result<UserOrderVO> getUserOrder(String id) {
        try {
            Long _id = DispatcherUtils.parseLong(id);
            if (null == _id)
                throw new Exception("请求参数错误");
            return lotteryServerFeignClient.getUserOrderH5(_id);
        } catch (Exception e) {
            log.error(e.toString());
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
    }

    @PostMapping("/zdCalc")
    @ApiOperation(value = "测试足彩中奖算法", notes = "测试足彩中奖算法")
    public Result<UserOrderVO> zdCalc() {
        try {
            return lotteryServerFeignClient.zdCalc();
        } catch (Exception e) {
            log.error(e.toString());
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
    }

    @PostMapping("/follow/add")
    @ApiOperation(value = "创建跟单", notes = "创建跟单")
    public Result addFollowOrder(@ApiParam(value = "用户Id", required = true) @RequestParam() Long userId,
                                 @ApiParam(value = "订单Id", required = true) @RequestParam() Long orderId,
                                 @ApiParam(value = "倍投次数", required = true) @RequestParam() Integer betTimes) {
        return lotteryServerFeignClient.addFollowOrder(userId,orderId,betTimes);
    }

    @PostMapping("/dj/page")
    @ApiOperation(value = "电竞订单列表查询", notes = "电竞订单列表查询")
    public Result queryDJUserOrderPage(UserOrderBO bo) {
        if (null == bo.getUserId())
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        if(null == bo.getStatus()){
            bo.setStatusList(Arrays.asList(0,1,2,3,4,5,6,7));
        }
        if(null != bo.getStatus() && 4==bo.getStatus()){
            bo.setStatus(null);
            bo.setStatusList(Arrays.asList(4,5,6));
        }
        if(null != bo.getStatus() &&2==bo.getStatus()){
            bo.setStatus(null);
            bo.setStatusList(Arrays.asList(2));
        }
        Result result = lotteryServerFeignClient.queryDJUserOrderPage(bo);
        return result;
    }


}
